﻿/// <reference path="../../extjs/ext-all-debug.js" />
Ext.define('yzmh.controller.ComicDatas', {
    extend: 'Ext.app.Controller',

    stores: ['ComicDatas'],

    models: ['ComicData'],

    views: ['comicdata.List', 'comicdata.Edit'],

    refs: [{
        ref: 'tbar',
        selector: 'comicdatalist toolbar'
    },{
        ref: 'comicdatalist',
        selector: 'comicdatalist'
    }],

    init: function () {
        this.control({
            'comicdatalist': {
                render: this.renderComicDataList,
                itemdblclick: this.edit
            },
            'comicdatalist button[action=add]': {
                click: this.add
            },
            'comicdatalist button[action=delete]': {
                click: this.del
            },
            'comicdatalist actioncolumn[action=view]': {
                click: this.view
            },
            'comicdatalist > treepanel treeview': {
                drop: this.drop,
                itemcontextmenu: function (view, rec, node, index, e) {
                    e.stopEvent();
                    this.getComicdatalist().contextMenu.showAt(e.getXY());
                    return false;
                }
            }
        });
    },

    drop:function (node, data, overModel, dropPosition, eOpts) {
        var comicdatalist = this.getComicdatalist();
        Ext.Msg.confirm("提示", "确定要将文件夹" + data.records[0].raw.path + "移动到" + data.records[0].parentNode.raw.path + "吗？", function (result) {
            if (result == "yes") {
                comicdatalist.mask();
                Ext.Ajax.request({
                    url: 'data/comicdata.aspx?a=drop',
                    params: { s: data.records[0].raw.path, d: data.records[0].parentNode.raw.path },
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        comicdatalist.unmask();
                        if (result.success) {
                            data.records[0].raw.path = data.records[0].parentNode.raw.path + "\\" + data.records[0].raw.text;
                            Ext.each(result.zjid.split(','), function (id) {
                                Ext.Ajax.request({
                                    url: 'data/makehtml.aspx?a=zj',
                                    params: { id: id },
                                    method: 'get'
                                });
                            });
                        } else {
                            notification('错误', result.errorMessage, true);
                            comicdatalist.items.each(function (tree) {
                                tree.getStore().load();
                            })
                        }
                    }
                });
            }
            else {
                comicdatalist.items.each(function (tree) {
                    tree.getStore().load();
                });
            }
        });
    },

    view: function (o, item, rowIndex, colIndex, e, row) {
        window.open("/list/" + row.get('filename'), '_blank');
    },

    add: function (grid, record) {
        var edit = Ext.create('yzmh.view.comicdata.Edit').show();
    },

    edit: function (grid, record) {
        var edit = Ext.create('yzmh.view.comicdata.Edit').show();
        edit.down('form').loadRecord(record);
    },

    del: function () {
        var comicdatalist = this.getComicdatalist();
        var paths = [];
        var nodes = [];
        comicdatalist.items.each(function (tree) {
            Ext.each(tree.getChecked(), function (node) {
                paths.push(node.raw.path);
                nodes.push(node);
            });
        });
        Ext.Msg.confirm("提示", "确定要删除选中的" + paths.length + "个文件吗？", function (result) {
            if (result == "yes") {
                Ext.Ajax.request({
                    url: 'data/comicdata.aspx?a=delete',
                    params: { path: paths.join('|') },
                    method: 'POST',
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        if (result.success) {
                            Ext.each(nodes, function (n) {
                                n.remove();
                            });
                        } else {
                            notification('错误', result.errorMessage, true);
                        }
                    }
                });
            }
        });
    },

    save: function (button, e) {
        var win = button.up('window'),
            form = win.down('form'),
            record = form.getRecord(),
            values = form.getValues(),
            store = this.getComicDatasStore();
        if (form.getForm().isValid()) {
            form.submit({
                url: 'data/comicdata.aspx?a=save',
                success: function (form, action) {
                    win.close();
                    store.reload();
                },
                failure: function (form, action) {
                    Ext.Msg.alert('Failed', action.result ? action.result.errorMessage : 'No response');
                }
            });
        }
    },

    renderComicDataList: function (panel) {
        this.getComicDatasStore().each(function (row) {
            panel.add({
                xtype: 'treepanel',
                icon: 'extjs/icons/folder_database.png',
                flex:1,
                rootVisible: false,
                title: row.get("name"),
                viewConfig: {
                    plugins: {
                        ptype: 'treeviewdragdrop',
                        appendOnly: true,
                        dragText: '{0} 个被选中的文件{1}'
                    }
                },
                store: {
                    xtype: 'treestore',
                    proxy: {
                        type: 'ajax',
                        url: 'data/comicdata.aspx?a=tree'
                    },
                    root: {
                        text: '图库',
                        expanded: true,
                        path: row.get('value')
                    },
                    folderSort: true,
                    sorters: [{
                        property: 'text',
                        direction: 'ASC'
                    }],
                    listeners: {
                        beforeload: function (ds, opration, opt) {
                            opration.params.path = opration.node.raw.path;
                        }
                    }
                }
            });
        });
    }
    
});
